<?php
	namespace Home\Controller;
	// use Think\Controller;
	class OrdersController extends EmptyController{
		
		//显示确认订单表
		public function index(){
			 //顶部判断，用户是否登录
	        $username = session('user');
	        
	        $this->assign('username',$username['username']);
			//实例化buycar表
			$Orders = D('buycar');
			//调用buycar类里的方法
			$list = $Orders->order_selc();
			// dump($list);
			//分配数据
			$this->assign('list',$list['list']);
			$this->assign('integral',$list['integral']);
            $this->assign('total',$list['total']);
            //查询用户默认地址
            $address = M('address');
            // 获取当前用户id
            $uid = session('user')['id'];
            // 设置查询条件
           	// $map['status'] = ['eq',1];
           	$map['uid'] = ['eq',$uid];
            $address = $address->where($map)->select();
            $this->assign('address',$address);
            //调用分类列表
            $this->type_secl();
            //调用友情链接
            $this->link_s();
            //调用导航栏
            $this->bar();
			//显示模板
			$this->display();
		}
		//订单完成方法
		public function flash(){
			 //顶部判断，用户是否登录
	        $username = session('user');
	        
	        $this->assign('username',$username['username']);
			//取得用户的收货地址信息
			$address = I('post.');
			$addressid = $address['id'];
			// dump($address);
			//判断地址是否为空
			if(!$address['linkman']||!$address['code']||!$address['phone']||!$address['address']||!$address['id']){
				$this->error('收货地址必须选');
			}
			//取得当前用户的id
			$uid = session('user')['id'];
			//获取用户购买商品信息
			$goods = M('buycar');
			//设置查询条件
			$map['status'] = ['eq',1];
			$glist = $goods->where($map)->select();
			//判断用户是否有选中商品
			if(!$glist){
				$this->error('您的订单没有商品');
			}
			// dump($glist);
			//随机生成一个唯一的订单号
			$orderid = mt_rand().time();
			//获取当前时间
			$addtime = time();
			
			//取得总价格
			$nums = count($glist);
			for ($i=0; $i < $nums; $i++) { 
				$total += $glist[$i]['total'];
				$integral = $glist[$i]['integral'];
			}
			// dump($total);
			//设置插入订单表数组
			$order['uid'] = $uid;
			$order['addressid'] = $addressid;
			$order['total'] = $total;
			$order['addtime'] = $addtime;
			$order['orderId'] = $orderid;
			//实例化订单表
			$orders = M('orders');
			$res = $orders->add($order);
		//用户表操作开始
			//实例化用户信息表
			$userInfo = M('user_info');
				//设置修改条件
			$map = [];
			$map['uid'] = ['eq',$uid];
			//查询用户原有积分
			$ulist = $userInfo->where($map)->find();
			$integral += $ulist['integral'];
			//给用户加上积分
			
			$userdata['integral'] = $integral;
			$userInfo->where($map)->save($userdata);
		//用户表操作结束
		//订单详情表，购物车表，商品表操作开始
			//实例化订单详情表
			$orderInfo = M('order_info');
			//实例化商品表
			$goodsInfo = M('goods');
			// 循环插入,修改删除
			for ($i=0; $i <$nums ; $i++) {
				//设置商品表修改条件
				$goodsI['id'] = $glist[$i]['gid'];
				//查询原有的购买量
				$Gnum = $goodsInfo->where($goodsI)->find();
				//设置要修改的东西
				$goods_Ip['clicknum'] = $glist[$i]['number'] + $Gnum['clicknum']; 
				//设置购物车表删除数据条件
				$bc_goods['id'] = $glist[$i]['id'];
				$bc_goods['status'] = $glist[$i]['status'];
				//设置订单详情表添加数据
				$data['orderid'] = $res;
				$data['uid'] = $uid;
				$data['num'] = $glist[$i]['number'];
				$data['goodsid'] = $glist[$i]['gid'];
				$data['price'] = $glist[$i]['price'];
				//执行order_info表添加
				$orderInfo->add($data);
				//执行buycar表删除
				$goods->where($bc_goods)->delete();
				//执行goods表修改
				$goodsInfo->where($goodsI)->save($goods_Ip);
			}
		//订单详情表，购物车表，商品表操作结束
			 //调用分类列表
            $this->type_secl();
            //调用友情链接
            $this->link_s();
            //调用导航栏
            $this->bar();
			$this->assign('orderid',$orderid);
			$this->assign('total',$total);
			$this->display();
		}
		//修改用户地址方法
		public function up_address(){
			//实例化address表
			$address = D('address');
			$res = $address->address_up();
			if($res){
				echo '修改成功';
			}else{
				echo '修改失败'; 
			}
		}
		//添加新的地址
		public function add_address(){
			$address = D('address');
			$res = $address->adds_address();
			if($res){
				echo '保存成功';
				// //实例化address表
				// $add = M('address');
				// //查询id 返回
				// $id = 
			}else{
				echo '保存失败';
			}
		}
		  //首页分类查询方法
		    public function type_secl(){
		       $type = D('type');
		       $t_list = $type->type_sec();
		       $this->assign('t_list',$t_list);
		    }
		    //首页友链查询方法
		    public function link_s(){
		        $links = D('links');
		        $link = $links->links_sec();
		        // dump($list);
		        $this->assign('link',$link);
		    }
		    //输出导航栏
		    public function bar(){
		        $type = D('type');
		        $t_bar = $type->bar_sec();
		        $this->assign('bar',$t_bar);
		    }
	}